package com.neusoft.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.domain.Food;
import com.neusoft.domain.PageResult;
import com.neusoft.domain.Vo.FoodVo;
import com.neusoft.mapper.FoodCategoryMapper;
import com.neusoft.mapper.FoodMapper;
import com.neusoft.service.FoodService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private FoodCategoryMapper foodCategoryMapper;
    @Override
    public PageResult<FoodVo> findFoodByPageAndShopId(Integer pageNum, Integer pageSize, String foodName, Integer shopId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Food>foods = foodMapper.findFoodByPageAndShopId(foodName, shopId);
        List<FoodVo>foodVos=new ArrayList<>();
        for (Food food : foods) {
            FoodVo foodVo = new FoodVo();
            BeanUtils.copyProperties(food,foodVo);
            foodVo.setCategoryName(foodCategoryMapper.selectCategoryName(food.getCategoryId()));
            foodVos.add(foodVo);
        }
        PageInfo<Food> PageResult = PageInfo.of(foods);
        return new PageResult<>(PageResult.getTotal(),foodVos);
    }

    @Override
    public void updateFood(Food food) {
        System.out.println(food);
        foodMapper.updateFood(food);
    }

    @Override
    public void addFood(Food food) {
        foodMapper.addFood(food);

    }

    @Override
    public void changeFoodStatus(Integer foodId, Integer status) {
        foodMapper.changeFoodStatus(foodId, status);
    }

    @Override
    public void recommendFood(Integer foodId, Integer recommend) {
        recommend ++;
        foodMapper.recommendFood(foodId, recommend);
    }

    @Override
    public void hotFood(Integer foodId, Integer hot) {
        Integer newHot=hot==0?1:0;
        foodMapper.hotFood(foodId, newHot);
    }
}
